package edu.ncu.controller;

import edu.ncu.entity.Friend;
import edu.ncu.entity.User;
import edu.ncu.service.FriendService;
import edu.ncu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/friend")
public class FriendController {
    final FriendService friendService;
    final UserService userService;

    public FriendController(FriendService friendService, UserService userService) {
        this.friendService = friendService;
        this.userService = userService;
    }
    // 跳转至好友列表页
    @RequestMapping("/")
    public String friends() {
        return "friendsPage" ;
    }



    @RequestMapping("/findAll")
    public String findAll(Model model)
    {
        System.out.println("表现层:findAll方法执行了...");
        //调用service方法,需要service层的对象,需要注入
        List<Friend> friends = friendService.findAll();
        System.out.println(friends);
        return "/index";
    }

    @RequestMapping("/findByUserId")
    public String findByUserId(int userId, ModelMap modelMap)
    {
        List<User> theFriends = friendService.findByUserId(userId);
        System.out.println("好友信息："+theFriends);
        User byUserId = userService.findByUserId(userId);
        modelMap.put("theFriends", theFriends);
        modelMap.put("user", byUserId);
        return "/friendsPage";
    }
}
